package com.pig4cloud.pig.item.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.item.api.bo.ItemBo;
import com.pig4cloud.pig.item.api.pojo.Item;
import com.pig4cloud.pig.item.service.ItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;



/**
 * @author pig code generator
 * @date 2020-10-13 10:09:07
 */
@RestController
@RequiredArgsConstructor
//@RequestMapping("/item" )
@Api(value = "item", tags = "管理")
public class ItemController {

    private final  ItemService itemService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param item
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/list/{page}/{limit}" )
    //@PreAuthorize("@pms.hasPermission('pig-item_item_get')" )
    public R getItemBOPage1(
            @PathVariable Integer page,
            @PathVariable Integer limit,
            Item item) {
        Page<Item> page1 = new Page<>(page, limit);
        QueryWrapper<Item> query = new QueryWrapper<>();
        if (item.getName() != null && StringUtils.isNotEmpty(item.getName())) {
            query.like("name", item.getName());
        }
        return R.ok(itemService.page(page1, query));
    }


    /**
     * 通过id查询
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    //@GetMapping("/{id}" )
    //@PreAuthorize("@pms.hasPermission('pig-item_item_get')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(itemService.getById(id));
    }

    @GetMapping("/{id}" )
    //@PreAuthorize("@pms.hasPermission('pig-item_item_get')" )
    public R getItemBo(@PathVariable("id" ) Integer id) {
        ItemBo itemBo = itemService.getItemBo(id);
        //categoryService.getById()
        return R.ok(itemService.getItemBo(id));
    }

    /**
     * 新增
     * @param item
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SysLog("新增" )
    @PostMapping("/save")
    //@PreAuthorize("@pms.hasPermission('pig-item_item_add')" )
    public R saveItemBO(@RequestBody Item item) {
        return R.ok(itemService.save(item));
    }

    /**
     * 修改
     * @param item
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SysLog("修改" )
    @PutMapping("/update")
    //@PreAuthorize("@pms.hasPermission('pig-item_item_edit')" )
    public R updateItemById(@RequestBody Item item) {
        return R.ok(itemService.updateById(item));
    }

    /**
     * 通过id删除
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @SysLog("通过id删除" )
    @DeleteMapping("/{id}" )
    //@PreAuthorize("@pms.hasPermission('pig-item_item_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(itemService.removeById(id));
    }

    //下面为代码生成器生成的代码

    /**
     * 分页查询
     * @param page 分页对象
     * @param item
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    //@PreAuthorize("@pms.hasPermission('pig-item_item_get')" )
    public R getItemPage(Page page, Item item) {
        return R.ok(itemService.page(page, Wrappers.query(item)));
    }

    /**
     * 新增
     * @param item
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SysLog("新增" )
    @PostMapping
    //@PreAuthorize("@pms.hasPermission('pig-item_item_add')" )
    public R save(@RequestBody Item item) {
        return R.ok(itemService.save(item));
    }

    /**
     * 修改
     * @param item
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SysLog("修改" )
    @PutMapping
    //@PreAuthorize("@pms.hasPermission('pig-item_item_edit')" )
    public R updateById(@RequestBody Item item) {
        return R.ok(itemService.updateById(item));
    }
}
